/*!
 * Component: components/Index/index
 * License: MIT
 * Created: 2020-03-02 16:24
 */

import { getAdvertising } from '/store/mutations'
import { GlobalData } from '/store/state'
import { $event } from '/utils'

Component({
  /**
   * Component properties
   */
  properties: {
    advertisingName: { type: String, value: '' },
    isProduct: { type: null, value: false },
    istheme: { type: null, value: false },
    isDetail: { type: null, value: false },
  },

  /**
   * Component initial data [private]
   */
  data: {
    advertisingList: [] as any[],
  },

  /**
   * lifecycle of component
   */
  lifetimes: {
    attached() {
      $event.on('after:boot', this, () => {
        this.getAdvertising()
      })
      this.getAdvertising()
    },
  },

  /**
   * Component methods
   */
  methods: {
    getAdvertising() {
      const advertisingList = getAdvertising(
        this.data.advertisingName,
        GlobalData.advertisingList,
        GlobalData.advertisingspace,
      )
      this.setData({ advertisingList })
    },

    toDetail(e: WechatMiniprogram.CustomEvent) {
      const link = e.currentTarget.dataset.link
      let linkType = ''
      let id = ''
      if (link != '') {
        linkType = link.split(':')[0]
        id = link.split(':')[1]
        if (linkType == 'https' || linkType == 'http' || linkType == 'link') {
          wx.navigateTo({
            url: `/pages/customweb/customweb?url=${link}`,
          })
        } else {
          switch (linkType) {
            case 'user':
              wx.navigateTo({
                url: `/mine/homePage/homePage?id=${id}`,
              })
              break
            case 'feed':
              wx.navigateTo({
                url: `/pages/feed/detail/detail?id=${id}`,
              })
              break
            case 'theme':
              wx.navigateTo({
                url: `/pages/theme/detail/detail?id=${id}`,
              })
              break
            case 'topic':
              wx.navigateTo({
                url: `/pages/topic/detail/detail?id=${id}`,
              })
              break
            case 'info':
              wx.navigateTo({
                url: `/index/information/detail/detail?id=${id}`,
              })
              break
            case 'commodity':
              wx.navigateTo({
                url: `/mall/detail/detail?commodityId=${id}`,
              })
              break
            case 'knowledge':
              wx.navigateTo({
                url: `/index/knowledge/detail/detail?id=${id}`,
              })
              break
            case 'activity':
              wx.navigateTo({
                url: `/pages/activity/detail/detail?id=${id}`,
              })
              break
            case 'qa':
              wx.navigateTo({
                url: `/index/knowAboutDetail/knowAboutDetail?id=${id}`,
              })
              break
            default:
              break
          }
        }
      }
    },
  },
})
